package com.lzhsite.mapreduce.sort;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/** 
 * 自定义分组有中方式，一种是继承WritableComparator 
 * 另外一种是实现RawComparator接口 
 * 
 * 分区和分组的区别
 * https://blog.csdn.net/asn_forever/article/details/81778972
 */  
public class DefinedGroupSort extends WritableComparator{  
  
  
    protected DefinedGroupSort() {  
        super(CombinationKey.class,true);  
    }  
  
    @Override  
    public int compare(WritableComparable a, WritableComparable b) {  
        System.out.println("---------------------进入自定义分组---------------------");  
        CombinationKey combinationKey1 = (CombinationKey) a;  
        CombinationKey combinationKey2 = (CombinationKey) b;  
        System.out.println("---------------------分组结果：" + combinationKey1.getFirstKey().compareTo(combinationKey2.getFirstKey()));  
        System.out.println("---------------------结束自定义分组---------------------");  
        //自定义按原始数据中第一个key分组  
        return combinationKey1.getFirstKey().compareTo(combinationKey2.getFirstKey());  
    }  
  
  
} 